package com.shasselclub.api.v1.service.impl;

import cn.hutool.core.date.DateUtil;
import com.shasselclub.api.v1.service.ValidParamService;
import com.shasselclub.common.constant.ResultModel;
import com.shasselclub.common.constant.ResultStatus;
import com.shasselclub.common.utils.DateUtils;
import com.shasselclub.common.utils.StringUtils;
import com.shasselclub.project.ask.question.domain.Question;
import com.shasselclub.project.ask.question.service.IQuestionService;
import com.shasselclub.project.ask.weixinUser.service.IWeixinUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @program: zntg_v1
 * @description:
 * @author: zhengyunfei
 * @create: 2018-10-30 15:48
 **/
@Service("validParamService")
@Slf4j
public class ValidParamServiceImpl implements ValidParamService {
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IWeixinUserService weixinUserService;
    /**
     * 校验参数
     * @param bindingResult
     * @param resultModel
     * @return
     */
    @Override
    public boolean validParam(BindingResult bindingResult, ResultModel resultModel) {
        AtomicBoolean isErrParam= new AtomicBoolean(true);
        if (bindingResult.hasErrors()) {
            bindingResult.getAllErrors().forEach(o -> {
                FieldError error = (FieldError) o;
                log.info(error.getField() + ":" + error.getDefaultMessage());
                if(isErrParam.get()){
                    resultModel.setMessage(error.getDefaultMessage());
                    resultModel.setCode(-1);
                    isErrParam.set(false);
                }
            });
        }
        if(!isErrParam.get()){
            return true;
        }
        return false;
    }

    /**
     *校验问卷id是否正确
     * @param questionId
     * @param resultModel
     * @return
     */
    @Override
    public boolean validParamForQuestionId(String  questionId, ResultModel resultModel) {
        //查询问卷信息
        Question question= questionService.selectQuestion();
        if(StringUtils.isNull(question)){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage("问卷配置信息错误");
            return true;
        }
        Date startTime=question.getStartTime();
        Date endTime=question.getEndTime();
        String hdId= DateUtil.format(startTime, DateUtils.YYYYMMDDHHMMSS)+DateUtil.format(endTime,DateUtils.YYYYMMDDHHMMSS);
        if(!questionId.equals(hdId)){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage("问卷id不正确");
            return true;
        }
        return false;
    }
    /**
     *校验openid是否正确
     * @param openId
     * @param resultModel
     * @return
     */
    @Override
    public boolean validParamForOpenId(String  openId, ResultModel resultModel) {
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("openId",openId);
        int count= weixinUserService.count(queryMap);
        if(count==0){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage("openId不正确");
            return true;
        }
        return false;
    }

    /**
     * 通过请求头判断是否是微信内置浏览器，是否是在微信内打开
     * @param request
     * @param resultModel
     * @return
     */
    @Override
    public boolean validParamForOpenId(HttpServletRequest request, ResultModel resultModel) {
        String ua = request.getHeader("user-agent")
                .toLowerCase();
        if (ua.indexOf("micromessenger") <= 0){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage("请在微信浏览器里面打开");
            return true;
        }
        return false;
    }


}
